package com.gitee.feizns.quickstart.ding.talk.api.v10.yida.forms.instances;

import com.aliyun.dingtalkyida_1_0.Client;
import com.aliyun.dingtalkyida_1_0.models.*;
import com.aliyun.teautil.models.RuntimeOptions;
import com.gitee.feizns.quickstart.ding.talk.client.Clients;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;

/**
 * 通过表单实例数据批量更新表单实例（/v1.0/yida/forms/instances/datas）
 * 接口路径：<a href="https://open.dingtalk.com/document/orgapp/update-multiple-form-instances-with-the-form-instance-data">通过表单实例数据批量更新表单实例（/v1.0/yida/forms/instances/datas）</a>
 * @link <a href="https://open.dingtalk.com/document/orgapp/update-multiple-form-instances-with-the-form-instance-data">服务端API 宜搭应用开发 表单 通过表单实例数据批量更新表单实例</a>
 * @author feizns
 * @since 2024/3/18
 */
public class Datas {

    /**
     * 执行根据编号获取表单实例数据
     * @param accessToken 访问凭据
     * @param req {@link BatchUpdateFormDataByInstanceMapRequest}
     * @return {@link List<String>}
     */
    @SneakyThrows
    public static List<String> execute(@NotNull String accessToken,
                                       @Nullable BatchUpdateFormDataByInstanceMapRequest req) {
        Client client = Clients.get(Client.class);
        BatchUpdateFormDataByInstanceMapResponse data = client.batchUpdateFormDataByInstanceMapWithOptions(req, headers(accessToken), new RuntimeOptions());
        return data.getBody().getResult();
    }

    /**
     * 执行根据编号获取表单实例数据
     *
     * @param accessToken                访问凭据
     * @param formUuid                   表格 UUID
     * @param appType                    应用类型
     * @param systemToken                系统令牌
     * @param updateFormDataJsonMap      更新表单数据JSON映射
     * @param noExecuteExpression        无执行表达式
     * @param asynchronousExecution      异步执行
     * @param ignoreEmpty                忽略空
     * @param useLatestFormSchemaVersion 使用最新表单架构版本
     * @return {@link List<String>}
     */
    @SneakyThrows
    public static List<String> execute(@NotNull String accessToken,
                                       @NotNull String formUuid,
                                       @NotNull String appType,
                                       @NotNull String systemToken,
                                       @NotNull Map<String, ?> updateFormDataJsonMap,
                                       @Nullable Boolean noExecuteExpression,
                                       @Nullable Boolean asynchronousExecution,
                                       @Nullable Boolean ignoreEmpty,
                                       @Nullable Boolean useLatestFormSchemaVersion) {
        BatchUpdateFormDataByInstanceMapRequest req = new BatchUpdateFormDataByInstanceMapRequest();
        req.setAppType(appType);
        req.setSystemToken(systemToken);
        req.setFormUuid(formUuid);
        req.setUpdateFormDataJsonMap(updateFormDataJsonMap);
        req.setNoExecuteExpression(noExecuteExpression);
        req.setAsynchronousExecution(asynchronousExecution);
        req.setIgnoreEmpty(ignoreEmpty);
        req.setUseLatestFormSchemaVersion(useLatestFormSchemaVersion);
        return execute(accessToken, req);
    }

    /**
     * @param accessToken 访问凭据
     * @return {@link BatchUpdateFormDataByInstanceMapHeaders}
     */
    public static BatchUpdateFormDataByInstanceMapHeaders headers(@NotNull String accessToken) {
        BatchUpdateFormDataByInstanceMapHeaders getFormDataByIDHeaders = new BatchUpdateFormDataByInstanceMapHeaders();
        getFormDataByIDHeaders.xAcsDingtalkAccessToken = accessToken;
        return getFormDataByIDHeaders;
    }

}
